package hotel;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ListIterator;
import java.util.Vector;

import javax.swing.*;

public class FinestraCheckIn extends JFrame implements ActionListener {
	private static ResultSet rs = null;
	ImageIcon iconax=new ImageIcon("Risorse/x4.png");
	private String camera;
	private String query1;
	private JPanel p, p1, p2;
	private int[] rows;
	private JButton riempi;
	private JScrollPane scroll;
	private String[] camere;
	private JList lista;
	private JLabel[] clienti;

	public FinestraCheckIn(int[] rows) {
		this.rows = rows;
		setSize(400, 400);
		setResizable(false);
		setLocation(350, 150);
		setVisible(true);
		p = new JPanel();
		p.setLayout(new BorderLayout());
		add(p);
		p1 = new JPanel();
		p1.setLayout(new GridLayout(20, 20, 0, 0));
		p2 = new JPanel();
		clienti = new JLabel[rows.length];
		if (rows.length > 1)
			p1.add(new JLabel("CLIENTI SELEZIONATI:"));
		else
			p1.add(new JLabel("CLIENTE SELEZIONATO:"));
		for (int i = 0; i < rows.length; i++) {
			clienti[i] = new JLabel(GraficaHotel.modelClienti.getValueAt(
					rows[i], 0)
					+ " "
					+ GraficaHotel.modelClienti.getValueAt(rows[i], 1));
			p1.add(clienti[i]);
		}
		p.add(p1, BorderLayout.WEST);
		p.add(p2, BorderLayout.EAST);
		camere = new String[GraficaHotel.modelCamere.getRowCount()];
		for (int i = 0; i < camere.length; i++) {
			camere[i] = (String) GraficaHotel.modelCamere.getValueAt(i, 0);
		}
		riempi = new JButton("Riempi");
		riempi.addActionListener(this);
		p.add(riempi, BorderLayout.SOUTH);
		lista = new JList(camere);
		scroll = new JScrollPane(lista);
		lista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		lista.setPreferredSize(new Dimension(100, 200));
		p2.add(new JLabel("CAMERE:"));
		p2.add(scroll);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == riempi) {
			if (lista.getSelectedValue() == null) {
				JOptionPane.showMessageDialog(null,
						"Seleziona una camera dalla lista.");
			}
			camera = (String) lista.getSelectedValue();
			query1 = "SELECT * FROM CAMERE where numero='" + camera + "'";

			for (int i = 0; i < clienti.length; i++) {
				try {
					rs = GraficaHotel.database.stat.executeQuery(query1);
				} catch (SQLException e2) {
					e2.printStackTrace();
				}
				try {
					if (rs.next())
						try {
							if (!controlloCamera()) {
								JOptionPane
										.showMessageDialog(null,
												"Nessun posto disponibile nella camera selezionata!","Errore",JOptionPane.WARNING_MESSAGE,iconax);

							} else if (controlloCliente(
									(String) GraficaHotel.modelClienti.getValueAt(
											rows[i], 3), i)) {
								rs = GraficaHotel.database.stat
										.executeQuery("SELECT camera FROM ASSOCIAZIONE where numerodocumento='"
												+ (String) GraficaHotel.modelClienti
														.getValueAt(rows[i], 3)
												+ "'");
								if (rs.next())
									JOptionPane.showMessageDialog(null,
											clienti[i].getText()
													+ " occupa già la camera "
													+ rs.getString(1) + "!","Errore",JOptionPane.WARNING_MESSAGE,iconax);
							} else {
								rs = GraficaHotel.database.stat
										.executeQuery(query1);
								if (rs.next())
									if (rs.getString(6)
											.compareTo("DISPONIBILE") == 0) {
										GraficaHotel.database.stat
												.executeUpdate("UPDATE CAMERE SET stato='OCCUPATA', numpostioccupati= numpostioccupati+1 where numero='"
														+ camera + "'");
										ListIterator<JButton> it = GraficaHotel.b
												.listIterator();
										boolean flag = true;
										while (it.hasNext() && flag) {
											JButton bottone = (JButton) it
													.next();
											if (bottone.getText().compareTo(
													camera) == 0) {
												bottone.setBackground(Color.RED);
												flag = false;
											}
										}
										aggiornaTabellaCamere();
									} else if (rs.getString(6).compareTo(
											"OCCUPATA") == 0)
										GraficaHotel.database.stat
												.executeUpdate("UPDATE CAMERE SET numpostioccupati= numpostioccupati+1 where numero='"
														+ camera + "'");
								GraficaHotel.database
										.query("INSERT INTO ASSOCIAZIONE VALUES ('"
												+ (String) GraficaHotel.modelClienti
														.getValueAt(rows[i], 3)
												+ "','" + camera + "')");
								ImageIcon icona=new ImageIcon("Risorse/v5.png");
								JOptionPane.showMessageDialog(null, "Cliente "
										+ clienti[i].getText()
										+ " registrato con successo!","Ottimo",JOptionPane.INFORMATION_MESSAGE,icona);

							}
						} catch (HeadlessException | SQLException | IOException e1) {
							e1.printStackTrace();
						}
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			dispose();

		}
	}

	private boolean controlloCamera() throws SQLException {
		if (rs.getString(6).compareTo("DISPONIBILE") == 0) {
			return true;
		} else if (rs.getString(6).compareTo("OCCUPATA") == 0
				&& controlloPosti()) {
			return true;
		} else if (rs.getString(6) == "PRENOTATA") {

		}
		return false;
	}

	private boolean controlloCliente(String query, int i) throws SQLException,
			HeadlessException, IOException {
		rs = GraficaHotel.database.stat
				.executeQuery("SELECT * FROM ASSOCIAZIONE where numerodocumento='"
						+ query + "'");
		return rs.next();
	}

	private boolean controlloPosti() throws SQLException {
		if (rs.getInt(3) == rs.getInt(4)) {
			return false;
		}
		return true;
	}

	public static void aggiornaTabellaCamere() throws SQLException {
		GraficaHotel.modelCamere.setRowCount(0);
		rs = GraficaHotel.database.stat.executeQuery("SELECT * FROM CAMERE");
		ResultSetMetaData md = rs.getMetaData();
		int columnCount = md.getColumnCount();
		Vector<String> row;
		while (rs.next()) {
			row = new Vector<String>(columnCount);
			for (int i = 2; i <= columnCount; i++) {
				if (i != 4)
					row.add(rs.getString(i));
			}
			GraficaHotel.modelCamere.addRow(row);
		}
		GraficaHotel.tabellaCamere.setModel(GraficaHotel.modelCamere);
	}

}
